package com.ftg.learn.amqp.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import kotlin.SubclassOptInRequired;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 布衣
 */
public class P {

    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.2.40.82");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/demo");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        channel.confirmSelect();

        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("发送成功 deliveryTag" + deliveryTag + "==multiple" + multiple);
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("发送失败 deliveryTag" + deliveryTag);
            }
        });

        String queueName = "working1";

        channel.queueDeclare(queueName, false, false, false, null);
        //工作队列没有交易所
        for (int i = 0; i < 10000; i++) {
            String message = "hello world112 " + 0;
            channel.basicPublish("", queueName, null, message.getBytes());
        }

        channel.waitForConfirms(2);
    }

}
